package com.cwj;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wujie.chen
 * 2021/11/3 15:26
 */
public class CreateTreeTest {
    private static Map<Integer, Node> trees = new HashMap<>();
    private static List<Role> data = new ArrayList<>();
    static {
        data.add(new Role(1, 0, "1"));
        data.add(new Role(2, 0, "2"));
        data.add(new Role(3, 1, "3"));
        data.add(new Role(4, 1, "4"));
        data.add(new Role(5, 2, "5"));
        data.add(new Role(6, 2, "6"));
        data.add(new Role(7, 2, "7"));
        data.add(new Role(8, 3, "8"));
        data.add(new Role(9, 4, "9"));
        data.add(new Role(10, 5, "10"));
        data.add(new Role(11, 6, "11"));
        data.add(new Role(12, 7, "12"));

    }


    /**
     * 1--
     *  3, 4
     *
     * 2 --
     *  5,6, 7
     * @param args
     */
    public static void main(String[] args) {
        List<Role> list = new ArrayList<>();
        list.add(new Role(8, 3, "8"));
        list.add(new Role(9, 4, "9"));
        list.add(new Role(10, 5, "10"));
        list.add(new Role(11, 6, "11"));
        list.add(new Role(12, 7, "12"));


        for (Role role : list) {
            Node parent = getParentNode(role, 0);
            if(parent!=null){
                parent.getList().add(new Node(role.getName(), 2, null));
            }
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            System.out.println(objectMapper.writeValueAsString(trees));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }
    private static Node getParentNode(Role role, int n){
        for (Role e : data) {
            if(e.id.equals(role.pid)){
                if(e.getPid().equals(0)){
                    List<Node> list = new ArrayList<>();
                    Node node = new Node(e.name, 0, list);
                    trees.put(e.id, node);
                    return node;
                }else{
                    Node parentNode = getParentNode(e, n + 1);
                    if(parentNode!=null){
                        parentNode.getList().add(new Node(e.name, n, new ArrayList<>()));
                    }
                    return parentNode;
                }
            }
        }
        return null;
    }


    public static class Node{
        private String level1;
        private String level2;
        private String level3;
        private List<Node> list;

        public Node() {
        }

        public Node(String level, int n, List<Node> list) {
            switch (n){
                case 0:
                    this.level1 = level;
                    break;
                case 1:
                    this.level2 = level;
                    break;
                case 2:
                    this.level3 = level;
                    break;
                default:
                    throw new RuntimeException("level异常");
            }
            this.list = list;
        }

        public String getLevel1() {
            return level1;
        }

        public void setLevel1(String level1) {
            this.level1 = level1;
        }

        public String getLevel2() {
            return level2;
        }

        public void setLevel2(String level2) {
            this.level2 = level2;
        }

        public String getLevel3() {
            return level3;
        }

        public void setLevel3(String level3) {
            this.level3 = level3;
        }

        public List<Node> getList() {
            return list;
        }

        public void setList(List<Node> list) {
            this.list = list;
        }

        @Override
        public String toString() {
            return "Node{" + "level1='" + level1 + '\'' + ", level2='" + level2 + '\'' + ", level3='" + level3 + '\''
                + ", list=" + list + '}';
        }
    }
    public static class Role{
        public Role(Integer id, Integer pid, String name){
            this.id = id;
            this.pid = pid;
            this.name = name;

        }

        private Integer id;
        private Integer pid;
        private String name;


        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public Integer getPid() {
            return pid;
        }

        public void setPid(Integer pid) {
            this.pid = pid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}


